package cn.org.timespace.plat.dao.mc.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.plat.action.mc.PlatMcInfoModel;
import cn.org.timespace.plat.dao.mc.PlatMcInfoDao;
import cn.org.timespace.plat.vo.mc.PlatMcInfo;

public class PlatMcInfoDaoImpl  extends TSBaseDAO implements PlatMcInfoDao{
	@Override
	public PlatMcInfo findById(String id) {
			// TODO Auto-generated method stub
			log.debug("getting PlatMcInfo instance with id: " +id);
			try{
				PlatMcInfo instance = (PlatMcInfo)getHibernateTemplate().get(
						"cn.org.timespace.plat.vo.mc.PlatMcInfo", id);
				return instance;
			}catch(RuntimeException re){
				log.debug("get fail",re);
				throw re;
		}
	}
	
	@Override
	public void save(PlatMcInfo instance) {
	
		log.debug("saving PlatMcInfo Instance");
		try{
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("saving successful");
		}catch(RuntimeException re){
			log.debug("saving fail",re);
			throw re;
		}
	}

	@Override
	public void delete(PlatMcInfo instance) {
		log.debug("deleting PlatMcInfo Instance");
		try{
			getHibernateTemplate().delete(instance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public void update(PlatMcInfo instance) {
		
		log.debug("update PlatMcInfo Instance");
		try{
			getHibernateTemplate().update(instance);
			
		}catch(RuntimeException re){
			log.error("update failed!",re);
			throw re;
		}	
	}

	@Override
	public List<PlatMcInfo> findByCondition(PlatMcInfoModel model) {
		log.debug("findByCondition PlatMcInfo Instance");
		StringBuffer queryString = new StringBuffer("from PlatMcInfo m where 1=1");
		if(!StringUtils.isEmpty(model.getMcId())){
			queryString.append("and m.mcid = '" + model.getMcId() + "'" );
		}
		if(!StringUtils.isEmpty(model.getStatus())){
			queryString.append("and m.mcid = '" + model.getStatus() + "'" );
		}
		queryString.append(" order by m.mcid desc");
		return getHibernateTemplate().find(queryString.toString());
	}

	
	
	@Override
	public List<Object> findByCondition(PlatMcInfoModel model, Pager p) {
	    log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from PlatMcInfo m where 1=1");
			if(!StringUtils.isEmpty(model.getMcId())){
				queryString.append("and m.mcId = '" + model.getMcId() + "'" );
			}
			if(!StringUtils.isEmpty(model.getStatus())){
				queryString.append("and m.status = '" + model.getStatus() + "'" );
			}
			if(!StringUtils.isEmpty(model.getConf())){
				queryString.append("and m.conf = '" + model.getConf() + "'" );
			}
			queryString.append(" order by m.mcId desc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
	
	@Override
	public PlatMcInfo findByMcId(String mcId) {
		log.debug("findByCondition PlatMcInfo Instance");
		StringBuffer queryString = new StringBuffer("from PlatMcInfo m where 1=1");
			queryString.append("and m.mcid = '" + mcId + "'" );
		    queryString.append(" order by m.mcid desc");
		List<PlatMcInfo> list = getHibernateTemplate().find(queryString.toString());
		if(list != null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
		
	}
	
}
